<!DOCTYPE html>
<html lang="en-us">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />

  	<meta property="og:title" content=" 新浪微博开放平台使用 &middot;  我的博客" />
  	<meta property="og:site_name" content="我的博客" />
  	<meta property="og:url" content="/my_technology/sanfang/" />
    
    
  	<meta property="og:type" content="article" />

    <meta property="og:article:published_time" content="2019-05-08T18:12:31&#43;08:00" />

    
    

  <title>
     新浪微博开放平台使用 &middot;  我的博客
  </title>

    <meta name="description" content="这里是付佳康的个人博客" />

    <meta name="HandheldFriendly" content="True" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="shortcut icon" href="/my_technology/images/favicon.ico">
	  <link rel="apple-touch-icon" href="/my_technology/images/apple-touch-icon.png" />
    
    <link rel="stylesheet" type="text/css" href="/my_technology/css/screen.css" />
    <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Merriweather:300,700,700italic,300italic|Open+Sans:700,400|Inconsolata" />


    
      
          <link href="/my_technology/index.xml" rel="alternate" type="application/rss+xml" title="我的博客" />
      
      
    
    <meta name="generator" content="Hugo 0.68.3" />

    <link rel="canonical" href="/my_technology/sanfang/" />

     
</head>
<body class="nav-closed">
<div id="particles-js"></div>
  


 <div class="site-wrapper">



<header class="main-header " style="background-image: url(/my_technology/images/user.jpg)">

    <nav class="main-nav overlay clearfix">
        
            <a class="blog-logo" href="/my_technology/"><img src="/my_technology/images/user.png" alt="Blog Logo" /></a>
        
        
    </nav>
<div class="vertical">
        <div class="main-header-content inner">
            <h1 class="page-title">
              <a class="btn-bootstrap-2" href="#content">我的博客</a>
          </h1>
          <h2 class="page-description">这里是付佳康的个人博客</h2>
        </div>
</div>
    <a class="scroll-down icon-arrow-left" href="#content"><span class="hidden">Scroll Down</span></a>
</header>

  <main id="content" class="content" role="main">


  <article class="post ">

    <header class="post-header">
        <h1 class="post-title">新浪微博开放平台使用</h1>
        <section class="post-meta">
        
         
        </section>
    </header>

    <section class="post-content">
      <pre><code>一、微博设置
  * 注册微博账号
  * 登录微博开放平台。https://open.weibo.com/
  * 进入首页。创建网站接入的应用。创建好以后，可以在“我的应用中查看”
  * 基本信息中的重要数据
      App Key：3035970630
      App Secret：cf1266f23c63aa9f65b3f9182b4e2eaa
  * 高级信息中配置“授权回调页” `http://127.0.0.1:8080/weibo_callback`

二、接口调用
* 查看接口文档： 点击“文档” -&gt; &quot;OAuth2.0授权认证&quot;

    接口 | 说明
    OAuth2/authorize | 请求用户授权Token
    OAuth2/access_token | 获取授权过的Access Token, UID


三、接口的使用
  OAuth2/authorize : 生成超链接，跳转到微博，让用户扫码登录（输入用户名、密码登录）
  url = &quot;https://api.weibo.com/oauth2/authorize?client_id=appkey&amp;redirect_uri=微博中配置的回调地址&quot;
  同意授权后会重定向
  http://www.example.com/response&amp;code=CODE


  VUE中编写回调接口,获取userToken
    http://127.0.0.1:8080/weibo_callback
  获取userToken,微博回调的Code
    this.$route.query[&quot;code&quot;];

  将Code发送到Django接口

    &lt;script&gt;
        export default {
            name: &quot;weibo_callback&quot;,
          data:function () {
              return {
                  token:&quot;&quot;
              }
          },
          mounted() {
              this.token = this.$route.query[&quot;code&quot;];

              this.axios({
              url: &quot;/api/app/weibo_login/&quot;,
              method: &quot;post&quot;,
              data:{&quot;token&quot;: this.token}
            }).then(res=&gt;{
              if(res.data.code==200){

              }
              else{

              }
            })
          }
        }
    &lt;/script&gt;


OAuth2/access_token 应，获取Uid(微博账号在新浪中到用户id)

    A  | 必选 | 类型及范围  |	说明
  client_id |	true    |   	string|	申请应用时分配的AppKey。
  client_secret |	true    |	string | 	申请应用时分配的AppSecret。
  grant_type |	true |	string | 	请求的类型，填写authorization_code
  code | 	true |	string	| 调用authorize获得的code值。
  redirect_uri | 	true |	string | 	回调地址，需需与注册应用里的回调地址一致。



接口返回值到官方例子

 {
       &quot;access_token&quot;: &quot;ACCESS_TOKEN&quot;,
       &quot;expires_in&quot;: 1234,
       &quot;remind_in&quot;:&quot;798114&quot;,
       &quot;uid&quot;:&quot;12341234&quot;
 }





    def post(self, request):
        code = request.data.get(&quot;token&quot;)
        
        # 获取uid,接口地址
        url = &quot;https://api.weibo.com/oauth2/access_token&quot;
        
        # 参数，具体含义见上表
        params = {
            &quot;client_id&quot;: &quot;2573333788&quot;,
            &quot;client_secret&quot;: &quot;fa96084f0937f1ef9a4cee5737895556&quot;,
            &quot;grant_type&quot;: &quot;authorization_code&quot;,
            &quot;code&quot;: code,
            &quot;redirect_uri&quot;: &quot;http://127.0.0.1:8080/weibo_callback&quot;
        }

        response = requests.post(url, params).text

        # 1、 从response中获取uid
        dict = json.loads(response)

        weibo_userid = dict[&quot;uid&quot;]

        # 2、 于本地数据库用的用户表做关联（微博绑定表）



        return Response(weibo_userid)


四、微博绑定
  将本地用户，与微博的uid绑定
  * 创建微博绑定表：uid(微博id)、user(用户外键)、type（三方类型）
  * request: 获取uid； response: 返回uid
  * vue创建微博绑定的页面（与注册页类似）。提交时：username、password、uid.
  * django接口，将用户名、密码，写入到用户表。将uid写入微博绑定表
  * django接口返回：登录成功、用户的Id



</code></pre>
    </section>


  <footer class="post-footer">


    
    <figure class="author-image">

        <a class="img" href="/my_technology/" style="background-image: url(/my_technology/images/user.png)"><span class="hidden">付佳康</span></a>
    </figure>
    

    <section class="author">

  <p>付佳康</p>
  

</section>


    
    <section class="share">
      <h4>Share this page</h4>
      <a class="icon-twitter" style="font-size: 1.4em" href="https://twitter.com/share?text=%e6%96%b0%e6%b5%aa%e5%be%ae%e5%8d%9a%e5%bc%80%e6%94%be%e5%b9%b3%e5%8f%b0%e4%bd%bf%e7%94%a8&amp;url=%2fmy_technology%2fsanfang%2f"
          onclick="window.open(this.href, 'twitter-share', 'width=550,height=235');return false;">
          <span class="hidden">Twitter</span>
      </a>
      <a class="icon-facebook" style="font-size: 1.4em" href="https://www.facebook.com/sharer/sharer.php?u=%2fmy_technology%2fsanfang%2f"
          onclick="window.open(this.href, 'facebook-share','width=580,height=296');return false;">
          <span class="hidden">Facebook</span>
      </a>
      <a class="icon-google-plus" style="font-size: 1.4em" href="https://plus.google.com/share?url=%2fmy_technology%2fsanfang%2f"
         onclick="window.open(this.href, 'google-plus-share', 'width=490,height=530');return false;">
          <span class="hidden">Google+</span>
      </a>
    </section>
    

    
    
    

  </footer>
</article>

</main>
    <footer class="site-footer clearfix">
        <section class="copyright"><a href="">我的博客</a> </section>
        
        <section class="poweredby">Proudly generated by <a class="icon-hugo" href="https://gohugo.io">HUGO</a>, with <a class="icon-theme" href="https://github.com/syui/hugo-theme-air">hugo-theme-air</a> theme</section>
        
    </footer>
    </div>
    <script type="text/javascript" src="/my_technology/js/jquery.js"></script>
    <script type="text/javascript" src="/my_technology/js/jquery.fitvids.js"></script>
    <script type="text/javascript" src="/my_technology/js/index.js"></script>
    <script src="/my_technology/js/particles.min.js"></script>
    <script src="/my_technology/js/particles.js"></script>  

</body>
</html>

